// 01login/reg/regPerfect/regPerfect.js
var co = require("../../../resource/js/common.js")
var Map = require("../../../resource/js/map.js")
var app = getApp()
var _this, _data
Page({
   data: {
      detail_place: '',
      wd: '',
      jd: '',
      PID: null, //省id
      CID: null, //市id
      AID: null, //区id
      Address_Detailed: "", //地址详情
      realName: "", //真实姓名
      carouselList: [], //轮播图列表
      radioValue:false
   },
   onLoad: function(options) {
      _this = this
      _data = this.data
   },
   //选择所在位置
   selectAddress(e) {
      wx.chooseLocation({
         success(res) {
            console.log("【允许授权】===", res)
            _this.setData({
               detail_place: res.address,
               Address_Detailed: res.name,
               wd: res.latitude,
               jd: res.longitude
            })
            var loc = {
               latitude: _data.wd,
               longitude: _data.jd
            }
            _this.getLocationName(loc)
         },
         fail(res) {
            if (res.errMsg.indexOf('auth') != -1) {
               wx.openSetting({
                  success: function(res) {
                     wx.setStorageSync("noMap", 1)
                  }
               })
            }
         }
      })
   },
   //用经纬度装省市区
   getLocationName(loc) {
      Map.getLocationName(loc, function(res) {
         if (res.status == 0) {
            var param = {}
            param.Province_Name = res.result.ad_info.province
            param.City_Name = res.result.ad_info.city
            param.Area_Name = res.result.ad_info.district
            _this.getPCACode(param)
         }
      })
   },
   //获取中国省市区(县)编码
   getPCACode(param) {
      co.getShareApi("api/Areas/getPCACode", param, function(res) {
         if (res.code == 0) {
            _this.setData({
               PID: res.data.PID,
               CID: res.data.CID,
               AID: res.data.AID
            })
            _this.intoAddress()
         }
      })
   },
   //获取真实姓名
   getRealName(e) {
      _data.realName = e.detail.value
   },
   //注册
   intoAddress() {
      co.getShareApi("address/Addrmg/saveAddress", {
         User_ID: app.Data.userInfo.User_ID,
         Receiver_Name: app.Data.userInfo.User_NickName,
         Users_ID: "jyhs",
         Address_Detailed: _data.Address_Detailed,
         Address_Province: _data.PID,
         Address_City: _data.CID,
         Address_Area: _data.AID,
         Address_Is_Default: 1,
         Addr_PrimaryLng: _data.jd,
         Addr_PrimaryLat: _data.wd
      }, function(res) {
         if (res.code == 0) {
            console.log(res)
         }
      })
   },
   //轮播图选择
   chooseCarouselImage: function(e) {
      wx.chooseImage({
         count: 1, // 最多可以选择的图片张数，默认9
         sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
         sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有          
         success: function(res) {
            console.log("【获取图片路径】===", res)
            //保存商家信息
            _this.uploadCarouselImg(res.tempFilePaths[0])
         },
      })
   },
   //轮播删除
   deleteCarouselImage: function(e) {
      var delImage = _data.carouselList.replace('http://h5.jyhs.ls293.cn', '')
      co.getJyhsApi('api/Upload/deleteImg', {
         imageFile: delImage
      }, function(res) {
         co.toastAllText(res.msg)
         if (res.code == 0) {
            _this.setData({
               carouselList: []
            })
         }
      })
   },
   //上传轮播
   uploadCarouselImg: function(img) {
      wx.showLoading({
         title: '图片上传中',
      })
      wx.uploadFile({
         url: 'https://api.jyhs.ls293.cn/api/Upload/img',
         filePath: img,
         name: 'image',
         formData: {
            dir: 'user_auth/' + app.Data.userInfo.User_ID
         },
         success: function(res) {
            //获取对象
            var obj = JSON.parse(res.data)
            _this.setData({
               carouselList: obj.data.wwwPath
            })
            wx.hideLoading()
         },
         fail: function(res) {
            wx.hideLoading()
            console.log('上传轮播图失败res=====', res)
         },
      })
   },
   //注册
   regJyhsb() {
      if (co.empty(_data.PID)){//地址不能为空
          return
      }
      if(co.empty(_data.realName)){
         return
      }
      if (co.empty(_data.carouselList)){
          return
      }
      if (_data.radioValue == false){
          co.toastAllText("请先勾起选框哦~")
      }else{
         co.getShareApi("jyhs/Usermg/updateInfo", {
            User_ID: app.Data.userInfo.User_ID,
            ID_Card_Name: _data.realName,
            ID_Card_Photo: _data.carouselList
         }, function (res) {
            if (res.code == 0) {
               //co.toastAllText(res.msg)
                wx.navigateTo({
                   url: '/01login/login',
                })
            }
         })
      }
   },
   radioChange: function (e) {
      var index = e.currentTarget.dataset.radio
      if(index == 0){
         _this.setData({
            radioValue: true
         })
      }
   },
   gotoAgreeMent() {
      wx.navigateTo({
         url: '/01login/reg/regPerfect/joinAgreement/joinAgreement',
         success:function(){
            _this.setData({
               radioValue: false
            })
         }
      })
   },
})